﻿namespace UnitTest.Dxp.Core

open System
open Microsoft.VisualStudio.TestTools.UnitTesting
open Lib.Dxp.Core.Serializer


type IZ() = 
    member val Id : int = 0         with get, set
    member val Name : string option = None   with get, set


type I() = 
    inherit IZ()
    member val Ida : int = 0            with get, set
    member val IdaXXX : int = 0         with get, set


type IST = {
    Id     : int 
    Name  : string
    }
type IST2 = {
    Id     : int 
    Name  : string option
    }

[<TestClass>]
type UnitTest_DxpSerial() = 

    [<TestMethod>]
    member x.serialRec () = 
        let i : IST = { Id = 1; Name = "xxx" }
        let access = Json.save(i)
        Assert.AreEqual("{\"Id@\":1,\"Name@\":\"xxx\"}", access)
    [<TestMethod>]
    member x.deserialRec () = 
        let r : IST = Json.load "{\"Id@\":1,\"Name@\":\"xxxy\"}"
        let i : IST = { Id = 1; Name = "xxxy" }
        Assert.AreEqual(sprintf "%A" i, sprintf "%A" r)
        Assert.AreEqual(1, r.Id)
        Assert.AreEqual("xxxy", r.Name)


    [<TestMethod>]
    member x.serialRec2 () = 
        let i = { Id = 1; Name = Some "xxx" }
        let access = Json.save(i)
        Assert.AreEqual("{\"Id@\":1,\"Name@\":{\"value\":\"xxx\"}}", access)
    [<TestMethod>]
    member x.deserialRec2 () = 
        let r : IST2 = Json.load "{\"Id@\":1,\"Name@\":{\"value\":\"xxx\"}}"
        let i : IST2 = { Id = 1; Name = Some "xxx" }
        Assert.AreEqual(sprintf "%A" i, sprintf "%A" r)
        Assert.AreEqual(1, r.Id)
        Assert.AreEqual(Some "xxx", r.Name)


    [<TestMethod>]
    member x.serial () = 
        let i = I ( Id = 1, Name = Some "xxx" )
        let access = Json.save(i)
        Assert.AreEqual("{\"Id@\":1,\"Name@\":{\"value\":\"xxx\"},\"IdaXXX@\":0,\"Ida@\":0}", access)
    [<TestMethod>]
    member x.deserial () = 
        let r : I = Json.load "{\"Id@\":1,\"Name@\":{\"value\":\"xxx\"},\"init@8-2\":1,\"IdaXXX@\":0,\"Ida@\":0,\"init@13-3\":1}"
        let i : I = I ( Id = 1, Name = Some "xxx" )
        Assert.AreEqual(sprintf "%A" i, sprintf "%A" r)
        Assert.AreEqual(1, r.Id)
        Assert.AreEqual(Some "xxx", r.Name)

